import BaseView from "../../cfw/mvc/BaseView";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName } from "../../public/Config";
import LoadingManager from "./LoadingManager";
import LoadingController from "./LoadingController";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoadingView extends BaseView {

    @property(cc.ProgressBar)
    progressBar: cc.ProgressBar = null;

    protected model: LoadingManager;

    protected controller: LoadingController;

    start() {
        GlobalEvent.instance().on(EventName.UPDATE_LOADING_PERCENT, this.updateProgress, this)
        this.model.start()
    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.UPDATE_LOADING_PERCENT, this.updateProgress, this)
        // ModuleManager.getLoader(ModuleID.LOADING).release()
    }

    updateProgress(percent: number) {
        cc.log(' percent ', percent)
        this.progressBar.progress = percent;
        if (percent >= 1) {
            this.controller.next()
        }
    }

    // update (dt) {}
}
